// 本地存储相关
export const localGet = (key) => {
  const value = window.localStorage.getItem(key)
  try {
    return JSON.parse(window.localStorage.getItem(key))
  } catch (error) {
    return value
  }
}

export const localSet = (key, value) => {
  window.localStorage.setItem(key, JSON.stringify(value))
}

export const localRemove = (key) => {
  window.localStorage.removeItem(key)
}

// 路径映射
export const pathMap = {
  personal: '个人信息',
  combos: '套餐管理',
  health: '健康管理',
  life: '生活管理',
  analytics: '数据分析',
  'staff-management': '人工管理',
  security: '安全管理',
  announcements: '公告管理',
  'medical-records': '病例记录管理',
  'medical-files': '病例档案管理',
  'outgoing-services': '外出服务管理',
  login: '登录'
}

// 格式化日期
export const formatDate = (date, format = 'YYYY-MM-DD HH:mm:ss') => {
  if (!date) return ''
  const d = new Date(date)
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  const hours = String(d.getHours()).padStart(2, '0')
  const minutes = String(d.getMinutes()).padStart(2, '0')
  const seconds = String(d.getSeconds()).padStart(2, '0')
  
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds)
}

// 防抖函数
export const debounce = (func, wait) => {
  let timeout
  return function executedFunction(...args) {
    const later = () => {
      clearTimeout(timeout)
      func(...args)
    }
    clearTimeout(timeout)
    timeout = setTimeout(later, wait)
  }
}

// 节流函数
export const throttle = (func, limit) => {
  let inThrottle
  return function() {
    const args = arguments
    const context = this
    if (!inThrottle) {
      func.apply(context, args)
      inThrottle = true
      setTimeout(() => inThrottle = false, limit)
    }
  }
}